package com.visionet.waveletData.web.web.robot;

import com.alibaba.fastjson.JSONObject;
import com.visionet.waveletData.web.common.security.password.RequestSignUtil;
import com.visionet.waveletData.web.entity.robot.Robot;
import com.visionet.waveletData.web.service.account.ShiroDbRealm;
import com.visionet.waveletData.web.service.robot.RobotService;
import com.visionet.waveletData.web.vo.RobotVo;
import com.visionet.waveletData.web.web.BaseController;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hxpeng on 2017/1/10.
 */

@Controller
@RequestMapping(value = "/robot")
public class RobotController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(RobotController.class);

    @Autowired
    private RobotService robotService;

    @RequestMapping(value = "/robotList", method = RequestMethod.POST)
    public @ResponseBody Map<String,Object> robotList(@RequestBody RobotVo robotVo){
        long starTime=System.currentTimeMillis();
        Map<String,Object> result = new HashMap<>();
        ShiroDbRealm.ShiroUser suser = (ShiroDbRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
        if(suser == null){
            throw new RuntimeException("未登录");
        }
        robotVo.setCompanyId(suser.getCompanyId());
        try {
            Page<RobotVo> page  = robotService.findAllRobotVo(robotVo);
            result.put("page",page);
        }catch (Exception e){
            logger.error("查询所有机器人异常");
            e.printStackTrace();
        }
        long endTime=System.currentTimeMillis();
        System.out.println("机器人列表时间："+ (endTime - starTime));
        return result;
    }

    @RequestMapping(value = "/selectRobotInGroupAdd", method = RequestMethod.POST)
    public @ResponseBody Map<String,Object> selectAllByRobotId(@RequestBody RobotVo robotVo){
        Map<String,Object> map = new HashMap<>();
        ShiroDbRealm.ShiroUser suser = (ShiroDbRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
        if(suser == null){
            throw new RuntimeException("未登录");
        }
        robotVo.setCompanyId(suser.getCompanyId());
        try {
            PageImpl<RobotVo> page = robotService.findPageRobotInGroupAddMethod(robotVo);
            map.put("page",page);
        }catch (Exception e){
            map.put("result","err");
            map.put("msg",e.getMessage());
        }
        return map;
    }


    @RequestMapping(value = "/delete/{robotNumber}", method = RequestMethod.GET)
    public @ResponseBody Map<String,Object> delete(@PathVariable String robotNumber){
        if(StringUtils.isBlank(robotNumber)){
            throw new NullPointerException("请求参数为空！！！");
        }
        Map<String,Object> map = new HashMap<>();
        try{
            map.put("result",robotService.deleteRobot(robotNumber));
        }catch (Exception e){
            logger.error("删除机器人异常：" + e.getMessage());
            map.put("result","err");
            map.put("msg",e.getMessage());
        }
        return map;
    }


    @RequestMapping(value ="/importRobotData", method = RequestMethod.POST)
    public @ResponseBody String importRobotData(@RequestBody JSONObject jsonObject) throws IOException {

        JSONObject result = new JSONObject();
        //校验签名
        if(!RequestSignUtil.checkSignByJSON(jsonObject)){
            result.put("code","90000");
            return result.toJSONString();
        }
        List<Robot> list = new ArrayList<>();
        String timestamp = jsonObject.getString("timestamp");
        String companyId = jsonObject.getString("companyId");
        String jsonArray = jsonObject.getString("robotList");
        String operateType = jsonObject.getString("operateType");
        if(StringUtils.isBlank(companyId)){
            result.put("code","90001");
            return result.toJSONString();
        }else if(StringUtils.isBlank(jsonArray)){
            result.put("code","90003");
            return result.toJSONString();
        }else if(StringUtils.isBlank(operateType) || (!operateType.equals("delete") && !operateType.equals("create"))){
            result.put("code","90004");
            return result.toJSONString();
        }
        try {
            System.out.println(jsonArray);
            list = JSONObject.parseArray(jsonArray,Robot.class);
        }catch (Exception e){
            result.put("code","90002");
            logger.error("解析导入机器人数组JSON字符错误");
            e.printStackTrace();
            return result.toJSONString();
        }
        logger.info("开始导入机器人----------------数量"+list.size()+"个");
        try {
            String resultMsg = robotService.importRobotData(list,companyId,operateType);
            result.put("code","10000");
            result.put("msg",resultMsg);
        }catch (Exception e){
            result.put("code","99999");
            logger.error("-------------导入机器人异常----------------");
            e.printStackTrace();
        }
        return result.toJSONString();
    }

}
